package com.ncr.travel.tas.configuration.model;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Type;
import org.hibernate.type.SerializationException;
import org.hibernate.util.SerializationHelper;

import com.thoughtworks.xstream.XStream;

@Entity
@Table(name = "configuration_value_template")
public class ConfigurationValueTemplate implements Serializable, ConfigurationModelObject {

	/**
	 *
	 */
	private static final long serialVersionUID = 3L;

	@Id
	@GeneratedValue
	@Column(name = "template_id")
	private long id;

	@Type(type = "text")
	private String regularExpression;

	private String defaultValue;

	@OneToOne(mappedBy = "template", fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
	private ConfigurationValue configurationValue;

	private String description;

	private String maximumValue;

	private String minimumValue;

	private boolean required;

	private boolean readOnly;

	private Class<?> type;

	/**
	 * @param value
	 * @throws SerializationException
	 */
	@Transient
	private byte[] serializeForDatabase(Object value) throws SerializationException {

		return SerializationHelper.serialize((Serializable) value);
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getId() {
		return id;
	}

	public void setConfigurationValue(ConfigurationValue configurationValue) {
		this.configurationValue = configurationValue;
	}

	public ConfigurationValue getConfigurationValue() {
		return configurationValue;
	}

	public void setRegularExpression(String regularExpression) {
		this.regularExpression = regularExpression;
	}

	public String getRegularExpression() {
		return regularExpression;
	}

	@Transient
	public void setDefaultValue(Object defaultValue) {

		if (defaultValue == null) {
			return;
		}

		String xml;
		XStream xstream = new XStream();
		xml = xstream.toXML(defaultValue);
		this.defaultValue = xml;
	}

	@Transient
	public Object getDefaultValue() {

		String value = this.defaultValue;
		if (value == null) {
			return null;
		}
		XStream xstream = new XStream();
		return xstream.fromXML(this.defaultValue);
	}

	@Transient
	public void setDescription(String description) {
		this.description = description;
	}

	@Transient
	public String getDescription() {
		return description;
	}

	@Transient
	public void setMaximumValue(Object value) {

		if (value == null) {
			return;
		}

		String xml;
		XStream xstream = new XStream();
		xml = xstream.toXML(value);
		this.maximumValue = xml;

	}

	@Transient
	public Object getMaximumValue() {

		String value = this.maximumValue;
		if (value == null) {
			return null;
		}
		XStream xstream = new XStream();
		return xstream.fromXML(this.maximumValue);

	}

	@Transient
	public void setMinimumValue(Object value) {

		if (value == null) {
			return;
		}

		String xml;
		XStream xstream = new XStream();
		xml = xstream.toXML(value);
		this.minimumValue = xml;

	}

	@Transient
	public Object getMinimumValue() {

		String value = this.minimumValue;
		if (value == null) {
			return null;
		}
		XStream xstream = new XStream();
		return xstream.fromXML(this.minimumValue);

	}

	public boolean isRequired() {
		return required;
	}

	public void setRequired(boolean required) {
		this.required = required;
	}

	public boolean isReadOnly() {
		return readOnly;
	}

	public void setReadOnly(boolean readOnly) {
		this.readOnly = readOnly;
	}

	public void setType(Class<?> type) {
		this.type = type;
	}

	public Class<?> getType() {
		return this.type;
	}
}
